# Maintainer: Marcelo Duarte https://github.com/marcelotduarte

_realname=lief
pkgbase=mingw-w64-python-${_realname}
pkgname=${MINGW_PACKAGE_PREFIX}-python-${_realname}
pkgdesc="LIEF - Library to Instrument Executable Formats (mingw-w64)"
pkgver=0.12.3
pkgrel=1
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://github.com/lief-project/LIEF/"
license=('spdx:Apache-2.0')
depends=("${MINGW_PACKAGE_PREFIX}-python")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-ccache"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-python-setuptools")
options=('staticlibs' '!strip' '!debug')
source=(lief-${pkgver}.zip::https://pypi.io/packages/source/l/lief/lief-${pkgver}.zip
        lief.patch)
sha256sums=('62e81d2f1a827d43152aed12446a604627e8833493a51dca027026eed0ce7128'
            'a85f8ae0cc72dd675bc6a9a5276f65c1136d07969976888b2609d378b4e81479')

prepare() {
  cd "${srcdir}/LIEF-${pkgver}"
  patch -p1 -i "${srcdir}/lief.patch"
  ${MINGW_PREFIX}/bin/python setup.py egg_info
}

build() {
  cd "${srcdir}/LIEF-${pkgver}"
  [[ -d build-${MSYSTEM} ]] && rm -rf build-${MSYSTEM}
  mkdir -p build-${MSYSTEM} && cd build-${MSYSTEM}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX" \
  ${MINGW_PREFIX}/bin/cmake.exe -LAH \
    -G"Ninja" \
    -DCMAKE_BUILD_TYPE="Release" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DLIEF_PYTHON_API=ON \
    -DLIEF_DISABLE_FROZEN=OFF \
    -DCMAKE_SKIP_RPATH=ON \
    -DLIEF_INSTALL_PYTHON=ON \
    -DPYTHON_EXECUTABLE=${MINGW_PREFIX}/bin/python.exe \
    -DCMAKE_VERBOSE_MAKEFILE=ON \
    -DCMAKE_C_USE_RESPONSE_FILE_FOR_OBJECTS=OFF \
    -DCMAKE_CXX_USE_RESPONSE_FILE_FOR_OBJECTS=OFF \
    -DLIEF_ELF=off -DLIEF_MACHO=off -DLIEF_OAT=off -DLIEF_DEX=off -DLIEF_VDEX=off -DLIEF_ART=off \
    ${_conf} \
    ..
    #-DBUILD_SHARED_LIBS:BOOL=OFF
    #-DBUILD_STATIC_LIBS:BOOL=OFF

  mkdir -p api/python/lief

  MSYS2_ARG_CONV_EXCL="--include-paths=;--typesystem-paths=" \
  ${MINGW_PREFIX}/bin/cmake.exe --build .
  
  MSYS2_ARG_CONV_EXCL="--include-paths=;--typesystem-paths=" \
  ${MINGW_PREFIX}/bin/cmake.exe --build . --target pyLIEF --config Release
}

package() {
  cd "${srcdir}/LIEF-${pkgver}"
  DESTDIR=${pkgdir} ${MINGW_PREFIX}/bin/cmake.exe --install build-${MSYSTEM}

  local _PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  for _f in "${pkgdir}${MINGW_PREFIX}"/lib/pkgconfig/*.pc; do
    sed -s "s|${_PREFIX_WIN}|${MINGW_PREFIX}|g" -i "${_f}"
  done

  # Install egg-info and .pyd
  _pythonpath=`${MINGW_PREFIX}/bin/python -c "import sysconfig; print(sysconfig.get_path('platlib'))"`
  _site_packages=${pkgdir}$(cygpath ${_pythonpath})
  _ext_suffix=`${MINGW_PREFIX}/bin/python -c "import sysconfig; print(sysconfig.get_config_var('EXT_SUFFIX'))"`
  mkdir -p $_site_packages
  cp -r ${_realname}.egg-info $_site_packages/${_realname}-${pkgver}.egg-info
  cp build-${MSYSTEM}/api/python/lief.pyd $_site_packages/lief$_ext_suffix

  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}

